﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace Sio.Mdm.Connector
{
    public class ldapObject : ICollection
    {
        #region Fields
        Dictionary<string, List<object>> _data = new Dictionary<string, List<object>>();
        List<String> _memberOf = new List<string>();
        string _CN;
        string _DN;
        string[] _objectClass;
        #endregion

        #region Properties
        
        public string CN
        {
            get 
            {
                return _CN;
            }
            set 
            {
                _CN = value;
            }
        }

        public string DN
        {
            get
            {
                return _DN;
            }
            set
            {
                _DN = value;
            }
        }

        public string[] objectClass
        {
            get
            {
                return _objectClass;
            }
            set
            {
                _objectClass = value;
            }
        }

        public List<string> MemberOf
        {
            get {
                return _memberOf;
            }
        }
        #endregion

        #region Indexer
        public List<object> this[string key]
        {
            get 
            {
                if (!Contains(key)) return null;
                return _data[key];
            }
        }
        #endregion

        #region Methods
        #region Public
        #region Constructor
        public ldapObject()
        {
                                       
        }
        public ldapObject(string DN)
        {
            _DN = DN;
        }
        #endregion

        #region Add
        public void Add(string key, object value)
        {
            if (key.ToUpper() == "CN") {            // This is special CASE, CN has direct getter
                CN = (string)value;
                return;
            }

            if (key.ToUpper() == "MEMBEROF")
            {
                _memberOf.Add(value.ToString());
                return;
            }

            if (_data.ContainsKey(key))
            {
                _data[key].Add(value);
            }
            else 
            {
                List<object> item = new List<object>();
                item.Add(value);
                _data.Add(key,item);
            }
        }
        #endregion

        public bool Contains(string key)
        {
            return _data.ContainsKey(key);
        }

        public List<string> GetKeys()
        {
            Dictionary<string, List<object>>.KeyCollection keysTemp = _data.Keys;

            List<string> keys = new List<string>();
            foreach (string s in keysTemp)
            {
                keys.Add(s);
            }

            return keys;
        }

        public void Remove(string key)
        {
            _data.Remove(key);
        }

        public void Clear()
        {
            _data.Clear();
            _memberOf.Clear();
        }

        public int Count()
        {
            return _data.Count;  
        }

        #region ICollection
        void ICollection.CopyTo(Array myArr, int index)
        {
            throw new NotImplementedException();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return new Enumerator(_data);
        }

        bool ICollection.IsSynchronized
        {
            get
            {
                return false;
            }
        }

        object ICollection.SyncRoot
        {
            get
            {
                return this;
            }
        }

        int ICollection.Count
        {
            get
            {
                return Count();
            }
        }
        #endregion

        #endregion
        #endregion
    }

    #region Class Enumerator
    public class Enumerator : IEnumerator
    {
        Dictionary<string, List<object>> _data;
        List<string> _keys;
        
        private int Cursor;

        public Enumerator(Dictionary<string, List<object>> data)
        {
            this._data = data;
            Dictionary<string, List<object>>.KeyCollection keysTemp = _data.Keys;

            _keys = new List<string>();
            foreach (string s in keysTemp)
            {
                _keys.Add(s);
            }

            Cursor = -1;
        }

        void IEnumerator.Reset()
        {
            Cursor = -1;
        }
        bool IEnumerator.MoveNext()
        {
            if (Cursor < _keys.Count)
                Cursor++;

            return (!(Cursor == _keys.Count));
        }

        object IEnumerator.Current
        {
            get
            {
                if ((Cursor < 0) || (Cursor == _keys.Count))
                    throw new InvalidOperationException();
                //return _data[_keys[Cursor]];
                return new KeyValuePair<string, List<object>>(_keys[Cursor], _data[_keys[Cursor]]);
            }
        }
    }
    #endregion
}
